package ${package.Controller};

import ${package.Entity}.${entity};
import ${package.Service}.${table.serviceName};
import ${cfg.configPack}.common.CommonPage;
import ${cfg.configPack}.common.CommonResult;
import ${cfg.configPack}.query.QueryGenerator;
import javax.servlet.http.HttpServletRequest;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.NotBlank;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.beans.factory.annotation.Autowired;

<#if restControllerStyle>
import org.springframework.web.bind.annotation.RestController;
<#else>
import org.springframework.stereotype.Controller;
</#if>
<#if superControllerClassPackage??>
import ${superControllerClassPackage};
</#if>


@Api(tags = "<#if package.ModuleName??>/${package.ModuleName?uncap_first}</#if>${table.controllerName}", description = "${table.comment!}管理")
<#if restControllerStyle>
@RestController
<#else>
@Controller
</#if>
@RequestMapping("<#if package.ModuleName??>/${package.ModuleName}</#if>/<#if controllerMappingHyphenStyle??>${controllerMappingHyphen}<#else>${table.entityPath}</#if>")
@Validated
<#if kotlin>
class ${table.controllerName}<#if superControllerClass??> : ${superControllerClass}()</#if>
<#else>
<#if superControllerClass??>
public class ${table.controllerName} extends ${superControllerClass} {
<#else>
public class ${table.controllerName} {
</#if>
<#assign parmPK=""/>
<#assign apiPK=""/>
<#assign actualParmPK=""/>
<#assign pathPK=""/>
<#list table.fields as field>
    <#if field.customMap.KEY??>
        <#if field.customMap.KEY == "PRI">
            <#assign pathPK += "/{" + field.propertyName + "}"/>
            <#if parmPK=="">
                <#assign parmPK += "@NotBlank@PathVariable(\"" + field.propertyName + "\") String " + field.propertyName/>
                <#assign apiPK += "@ApiImplicitParam(name = \""+ field.propertyName +"\", value = \"" + field.comment + "\", required = true, dataType = \"String\", paramType = \"path\")"/>
                <#assign actualParmPK += field.propertyName/>
            <#else>
                <#assign parmPK += ",\n\t\t\t\t\t\t@NotBlank@PathVariable(\""+ field.propertyName +"\") String " + field.propertyName/>
                <#assign apiPK += ",\n\t\t@ApiImplicitParam(name = \""+ field.propertyName +"\", value = \"" + field.comment + "\", required = true, dataType = \"String\", paramType = \"path\")"/>
                <#assign actualParmPK += ", " + field.propertyName/>
            </#if>
        </#if>
    </#if>
</#list>

    @Autowired
    ${table.serviceName} ${table.serviceName?substring(1)?uncap_first};

    @ApiOperation("${table.comment!}分页列表")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "pageNo", value = "当前页", required = true, dataType = "Integer", paramType = "query"),
        @ApiImplicitParam(name = "pageSize", value = "每页数量", required = true, dataType = "Integer", paramType = "query"),
    })
    @GetMapping("/pageInfo")
    public CommonPage<${entity}> pageInfo(@NotNull Integer pageNo,
                                   @NotNull Integer pageSize,
                                   ${entity} ${entity?uncap_first},
                                   HttpServletRequest request){
        Page<${entity}> pageInfo = new Page<>(pageNo, pageSize);
        QueryWrapper<${entity}> qw = QueryGenerator.initQueryWrapper(${entity?uncap_first}, request.getParameterMap());
        Page<${entity}> page1 = ${table.serviceName?substring(1)?uncap_first}.page(pageInfo, qw);
        return CommonPage.restPage(page1);
    }

    @ApiOperation("通过主键获取${table.comment!}")
    @ApiImplicitParams({
        ${apiPK}
    })
    @GetMapping("${pathPK}")
    public CommonResult<${entity}> getByPk(${parmPK}){
        ${entity} ${entity?uncap_first} = ${table.serviceName?substring(1)?uncap_first}.getByPk(${actualParmPK});
        return CommonResult.success(${entity?uncap_first});
    }

    @ApiOperation("新增${table.comment!}")
    @PostMapping
    public CommonResult add(@Validated ${entity} ${entity?uncap_first}, BindingResult bindingResult){
    boolean b = ${table.serviceName?substring(1)?uncap_first}.save(${entity?uncap_first});
        if (b){
            return CommonResult.success("成功");
        }
        return CommonResult.failed("失败");
    }

    @ApiOperation("修改${table.comment!}")
    @PutMapping
    public CommonResult updateByPk(@Validated ${entity} ${entity?uncap_first}, BindingResult bindingResult){
        boolean b = ${table.serviceName?substring(1)?uncap_first}.updateByPk(${entity?uncap_first});
        if (b){
            return CommonResult.success("成功");
        }
        return CommonResult.failed("失败");
    }

    @ApiOperation("通过主键删除${table.comment!}")
    @ApiImplicitParams({
        ${apiPK}
    })
    @DeleteMapping("${pathPK}")
    public CommonResult removeByPk(${parmPK}){
        boolean b = ${table.serviceName?substring(1)?uncap_first}.removeByPk(${actualParmPK});
        if (b){
            return CommonResult.success("成功");
        }
        return CommonResult.failed("失败");
    }



}
</#if>
